<?php

namespace app\wechat\controller;

use think\Config;
use think\Db;
use app\enumerations\OrderStatusEnum;

class Order extends Common {

    use WechatTrait;

    /**
     * 订单列表
     * @return \think\response\Json
     */
    public function myOrderList() {

        $currentCustomer = $this->request->currentCustomer;
        $pageNum = $this->request->post('pageNum');
        $pageSize = $this->request->post('pageSize');

        $list = $this->orderService->getOrderListForPage(
            $currentCustomer['id'], $pageNum, $pageSize);

        // 拼接商品ID字符串
        $goodsIds = '';
        foreach ($list as &$val) {

            $goodsIds .= $val['goods_id'].',';

//            $val['order_create_time'] = substr($val['handle_time'],
//                0, strlen($val['handle_time'])-3);
            $val['order_create_time'] = $val['yx_create_time'];
            $val['order_status_name'] = getEnumString(
                OrderStatusEnum::getAllList(), $val['order_status']);
//            $val['commission'] = number_format($val['commission']/100,
//                2, '.', '');

            unset($val['handle_time']);
        }

        $goodsIds = substr($goodsIds, 0, strlen($goodsIds)-1);

        //每日优选的
        $yxGoodsResult = getYxGoodsSearchList(1, $pageSize,
            '', $goodsIds);

        if(!$yxGoodsResult["success"]) {
            $returnError = config("errorWechat.errorYxService");
            $returnError["message"] = $yxGoodsResult["msg"];
            return json($returnError);
        }

        foreach ($yxGoodsResult['list']['list'] as $key=>$val) {

            foreach ($list as $k=>$v) {

                if ($val['goodsId'] == $v['goods_id']) {

                    $v['goods_name'] = $val['goodsName'];
                    $v['little_desc'] = $val['littleDesc'];
                    $v['shop_price'] = $val['shopPrice'];
                    $v['market_price'] = $val['marketPrice'];
                    $v['goods_img'] = $val['goodsImg'];
                    $v['sale_num'] = $val['saleNum'];
                }
            }
        }

        $returnData['list'] = $list;

        return successReturn($returnData);

    }

    /**
     * 新版订单列表
     * @return \think\response\Json
     */
    public function orderList() {

        $currentCustomer = $this->request->currentCustomer;
        $keyword = $this->request->post("keyword");
        $pageNum = $this->request->post('pageNum');
        $pageSize = $this->request->post('pageSize');
        $orderStatus = $this->request->post("orderStatus");

        // 如果订单状态不为空
        if (!isNullOrEmpty($orderStatus)) {

            if (!checkEnumValue($orderStatus, OrderStatusEnum::getAllList())) {
                return errorReturn("errorWechat.errorOrderStatusValue");
            }
        }

        $list = $this->orderService->getOrderListByKeywordAndOrderStatusForPage(
            $currentCustomer['id'], $keyword, $orderStatus, $pageNum, $pageSize);

        // 拼接商品ID字符串
        $goodsIds = '';
        foreach ($list as &$val) {

            $goodsIds .= $val['goods_id'].',';

            $val['order_create_time'] = $val['yx_create_time'];
            $val['order_status_name'] = getEnumString(
                OrderStatusEnum::getAllList(), $val['order_status']);

            unset($val['handle_time']);
        }

        $goodsIds = substr($goodsIds, 0, strlen($goodsIds)-1);

        //每日优选的
        $yxGoodsResult = getYxGoodsSearchList(1, $pageSize,
            '', $goodsIds);

        if(!$yxGoodsResult["success"]) {
            $returnError = config("errorWechat.errorYxService");
            $returnError["message"] = $yxGoodsResult["msg"];
            return json($returnError);
        }

        foreach ($yxGoodsResult['list']['list'] as $key=>$val) {

            foreach ($list as $k=>$v) {

                if ($val['goodsId'] == $v['goods_id']) {

                    $v['goods_name'] = $val['goodsName'];
                    $v['little_desc'] = $val['littleDesc'];
                    $v['shop_price'] = $val['shopPrice'];
                    $v['market_price'] = $val['marketPrice'];
                    $v['goods_img'] = $val['goodsImg'];
                    $v['sale_num'] = $val['saleNum'];
                }
            }
        }

        // 累计售出多少单 有效的
        $totalOrderCount = $this->orderService
            ->countValidByCustomerId($currentCustomer['id']);

        // 待发货订单数量
        $waitDeliverGoodsOrderCount = $this->orderService
            ->countOrderByCustomerIdAndOrderStatus($currentCustomer['id'],
                OrderStatusEnum::getEnumValue(OrderStatusEnum::WAITDELIVERGOODS_CODE));

        // 已发货订单数量
        $distributionOrderCount = $this->orderService
            ->countOrderByCustomerIdAndOrderStatus($currentCustomer['id'],
                OrderStatusEnum::getEnumValue(OrderStatusEnum::DISTRIBUTION_CODE));

        // 确认收货订单数量
        $confirmCollectGoodsOrderCount = $this->orderService
            ->countOrderByCustomerIdAndOrderStatus($currentCustomer['id'],
                OrderStatusEnum::getEnumValue(OrderStatusEnum::CONFIRMCOLLECTGOODS_CODE));

        // 查询当前用户昨天统计数据
        $yesterdayStatistics = $this->customerEverydayStatisticsService
            ->findByCustomerIdAndDay($currentCustomer['id'], date("Y-m-d", strtotime("-1 day")));

        $returnData['info'] = $this->baseCustomerInfo();
        $returnData['info']['total_order_count'] = $totalOrderCount;
        $returnData['info']['yesterday_order_count'] = isNullOrEmpty($yesterdayStatistics['valid_order_count'])
            ? 0 : $yesterdayStatistics['valid_order_count'];
        $returnData['info']['wait_deliver_goods_order_count'] = $waitDeliverGoodsOrderCount;
        $returnData['info']['distribution_order_count'] = $distributionOrderCount;
        $returnData['info']['confirm_collect_goods_order_count'] = $confirmCollectGoodsOrderCount;
        $returnData['list'] = $list;

        return successReturn($returnData);

    }

}